import requests
from datetime import datetime

# 主函数，用于检测换向阀指示灯故障并推送故障信息
def MianB_huanxiang_fa():
    # 定义设备数据，包含流量、压力等信息
    data = {
        "results": [
            {
                "id": 1,
                "liu_liang_ji": "27.3625",
                "wan_neng_ya_li": "27.74375",
                "guan_hui_ya_li": "3000",
                "chu_neng_qi_ya_li": "3000",
                "zuan_ji_qi_ya": "3000",
                "pang_tong": {
                    "hong_yali": False,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "wan_neng": {
                    "hong_yali": False,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "dian_beng_1": {
                    "deng": True,
                },
                "dian_beng_2": {
                    "deng": True,
                },
                "shang_ya_jing": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "ke_bian_zha_ban": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "shang_zu_liu": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "xia_ya_jing": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "jian_qie_zha_ban": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "xia_zu_liu": {
                    "hong_yali": True,
                    "hong_deng": False,
                    "lv_yali": False,
                    "lv_deng": True
                },
                "bei_yong_fa": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "shang_zha_ban": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "xia_zha_ban": {
                    "hong_yali": True,
                    "hong_deng": True,
                    "lv_yali": True,
                    "lv_deng": True
                },
                "jin_ji_dian_yuan": {
                    "deng": True,
                },
                "div_tong_xin": {
                    "deng": True,
                },
                "HPU_tong_xin": {
                    "deng": True,
                },
                "bao_jing": {
                    "deng": True,
                },
                "lei_ji_power_low": {
                    "deng": True,
                },
                "ye_wei_low": {
                    "deng": True,
                },
                "zuan_ji_qi_ya_low": {
                    "deng": True,
                },
                "beng_ya_li_low": {
                    "deng": True,
                },
                "updated": "2025-02-14T10:50:34.521938",
                "created": "2025-02-14T10:50:34.521938"
            }
        ]
    }

    functions = [
        {
            "name": "pang_tong",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "shang_ya_jing",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "ke_bian_zha_ban",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "shang_ya_jing",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "shang_zu_liu",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "xia_ya_jing",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "jian_qie_zha_ban",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "xia_zu_liu",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "shang_zha_ban",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "wan_neng",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },
        {
            "name": "xia_zha_ban",
            "pressure_signals": ["hong_yali", "lv_yali"],
            "indicator_signals": ["hong_deng", "lv_deng"]
        },

    ]

    # 获取当前主流量，并转换为浮点数
    main_flow_rate = float(data["results"][0]["liu_liang_ji"])
    # 初始化上一次的主流量为 0
    last_main_flow_rate = 0

    # 遍历每个需要检测的设备
    for func in functions:
        # 获取当前设备的数据
        func_data = data["results"][0][func["name"]]
        # 遍历当前设备的每个压力信号和指示灯信号
        for i in range(len(func["pressure_signals"])):
            # 获取当前压力信号的值
            pressure_signal = func_data[func["pressure_signals"][i]]
            # 获取当前指示灯信号的值
            indicator_signal = func_data[func["indicator_signals"][i]]
            # 判断当前主流量是否大于上一次的主流量
            flow_increased = main_flow_rate > last_main_flow_rate

            # 如果压力信号和指示灯信号不一致，并且主流量增加了
            if pressure_signal != indicator_signal and flow_increased:
                # 打印指示灯损坏的信息
                print(f"{func['name']} 的 {func['indicator_signals'][i]} 指示灯损坏")

                # 构造故障推送数据
                fault_data = {
                    "gu_zhang_ming_cheng": "指示灯损坏",
                    "she_bei_ming_cheng": func["name"],
                    "she_bei_qu_yu": "换向阀组",  # 根据实际设备区域调整
                    "gu_zhang_shi_jian": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"),
                    "gu_zhang_lei_xing": "设备故障",
                    "shi_ji_zhi": int(indicator_signal) if isinstance(indicator_signal, bool) else indicator_signal,
                    "gao_jing_zhi": int(pressure_signal) if isinstance(pressure_signal, bool) else pressure_signal,
                    "gu_zhang_miao_shu": f"{func['name']}的{func['indicator_signals'][i]}指示灯状态（{indicator_signal}）与压力信号（{pressure_signal}）不一致",
                    "chu_li_zhuang_tai": "未处理"
                }

                # 发送 POST 请求到 Django API
                try:
                    response = requests.post(
                        "http://222.134.245.144:17000/weizhang/VioModel/",
                        json=fault_data
                    )
                    # 判断请求是否成功
                    if response.status_code == 201:
                        # 打印故障推送成功的信息
                        print(f"故障推送成功：{fault_data['gu_zhang_miao_shu']}")
                    else:
                        # 打印故障推送失败的信息
                        print(f"故障推送失败，状态码：{response.status_code}")
                except requests.exceptions.RequestException as e:
                    # 打印网络请求失败的信息
                    print(f"网络请求失败：{str(e)}")

# 调用主函数
MianB_huanxiang_fa()